﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;

namespace Baosight.ZNH.ZM.UI
{
    public partial class ZM02 : EFView
    {
        public ZM02()
        {
            InitializeComponent();
        }

        public override void Initialize(EIInfo initData)
        {
            base.Initialize(initData);

            string codeset = null;

            if (initData.Attributes.ContainsKey("codeset"))
            {
                codeset = initData.Get("codeset") as string;
            }

            //如果是小代码，不需要查询和翻页
            if (codeset != null && !String.IsNullOrEmpty(codeset.Trim()))
            {
                this.code.Visibility = Visibility.Collapsed;
                this.name.Visibility = Visibility.Collapsed;
                this.query.Visibility = Visibility.Collapsed;
                this.codeGrid.eCanUserPageRecords = false;
                this.codeGrid.UpdateData(initData);
            }
            else
            {
                string labelName = initData.Get("displayMemberPath") as string;
                string valueName = initData.Get("valueMemberPath") as string;
                string bindingService = initData.Get("bindingService") as string;
                var tokens = bindingService.Split('.');
                this.code.eBindingName = "inqu_status-0-" + valueName;
                this.name.eBindingName = "inqu_status-0-" + labelName;

                //如果从initLoad方法中返回了元数据，取得查询条件列的中文名
                if (initData.Blocks.ContainsKey("result"))
                {
                    var block = initData.Blocks["result"];
                    var cols = new List<EIColumn>();
                    if (block.Columns.ContainsKey(valueName))
                    {
                        this.code.eLabel = block.Columns[valueName].Desc;
                        cols.Add(block.Columns[valueName]);
                    }
                    if (block.Columns.ContainsKey(labelName))
                    {
                        this.name.eLabel = block.Columns[labelName].Desc;
                        cols.Add(block.Columns[labelName]);
                    }
                    if (cols.Count > 0)
                    {
                        this.codeGrid.eAutoDraw = false;
                        foreach (var eic in cols)
                        {
                            var item = new EFColumn();
                            item.eHeader = eic.Desc;
                            item.eColumnName = eic.Name;
                            item.eName = eic.Name;
                            item.eWidth = 200;
                            this.codeGrid.Columns.Add(item);
                        }
                    }
                }

                if (tokens.Length == 2)
                {
                    this.eServiceName = tokens[0];
                    this.eMethodName = tokens[1];

                    this.codeGrid.eServiceName = tokens[0];
                    this.codeGrid.eMethodName = tokens[1];
                }
            }
        }


        private void query_Click(object sender, RoutedEventArgs e)
        {
            base.OnQueryButtonClick(sender, e);
        }

        private void clean_Click(object sender, RoutedEventArgs e)
        {            
            name.Value = "";
            code.Value = "";
        }

        private void sure_Click(object sender, RoutedEventArgs e)
        {
            List<EIRow> rows = this.codeGrid.CheckedEIRows;
            EFWindow wid = this.Parent as EFWindow;
            wid.DialogResult = true;
            wid.Content = rows;
            wid.Close();
        }

        private void cancel_Click(object sender, RoutedEventArgs e)
        {
            EFWindow wid = this.Parent as EFWindow;
            wid.Close();
        }

    }
}
